﻿using ADODB;
using fuzhu;
using LH.Model.ST;
using SqlSuarEntity;
using SqlSugar;
using System.Data;
using System.Runtime.ConstrainedExecution;
using System.Security.Cryptography;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.AS;
using XXX.Models.XXXEntities;
using XXX.Models.ZJGLX;
using ZDEntity;
using static XXX.Models.MO.API_Morder;

namespace Yonyou.U8.BO
{
    /// <summary>
    /// 客户调价单
    /// </summary>
    public class CusPriceAdjustVM
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }

        public string cacc { get; set; }
        public string xmlConfig { get; set; }
        public bool bWMS = false;
        public string cDatabase = "";
        public SA_CusPriceJustMain head ;
        public List<SA_CusPriceJustDetail> body;
       

        public CusPriceAdjustVM()
        {
            //conStr
            conStr = CanShu.conStr;
            cacc = CanShu.cZT;
            //初始化定义xmlconfig
            //xmlConfig = XmlOperation.GetConfigXML();
        }
         
        public RetModel2 AddVerify()
        {
            try
            {
                string idcheck = "";
               
                    //取得账套号
                    string cAcc = cacc;
                int cnt = body.Count;
                //id值减去获取条件，后续在新增
                int[] arrayMoid = DbHelper2.GetU8ID(cAcc, "SA_CusPriceJustMain", cnt, conStr);
                    int iid = arrayMoid[0] - 1;
                int iautoid = arrayMoid[1] - cnt;

                iid += 1;

                head.id = iid;
                 List<SA_CusUPrice> cusPriceEntity = new List<SA_CusUPrice>();
                List<SA_CusUPrice> cusPriceEntityModify = new List<SA_CusUPrice>();
              
                SqlSugarClient Tardb = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                });
                try
                {
                    for (int i = 0; i < body.Count; i++)
                    {
                        SA_CusUPrice sA_CusUPrice = new SA_CusUPrice();

                        iautoid += 1;
                    body[i].id = iid;
                    body[i].autoid = iautoid;
                    sA_CusUPrice.cInvCode =body[i].cinvcode;
                    sA_CusUPrice.cCusCode = body[i].ccuscode;
                    sA_CusUPrice.cCusCCode = body[i].ccusccode;
                    sA_CusUPrice.fminquantity = body[i].fminquantity;
                    sA_CusUPrice.dStartDate = body[i].dstartdate;
                    sA_CusUPrice.dEndDate = body[i].denddate;
                    sA_CusUPrice.bsales = body[i].bsales;
                    sA_CusUPrice.iInvNowCost = body[i].iinvnowcost;
                    sA_CusUPrice.fcusminprice = body[i].fcusminprice;
                    sA_CusUPrice.iInvSCost = body[i].iinvscost;
                    sA_CusUPrice.iInvNowCost = body[i].iinvnowcost;
                    sA_CusUPrice.icusjustautoid = iautoid;
                    sA_CusUPrice.iCusDisRate= body[i].icusdisrate;
                    sA_CusUPrice.cexch_name = body[i].cexch_name;
                    sA_CusUPrice.binvalid = false;
                    //不含税
                    sA_CusUPrice.bsaleprice = false;

                    //根据新增和修改，分别加到不同的list
                    if (body[i].operationtype == "新增")
                    {
                        cusPriceEntity.Add(sA_CusUPrice);
                    }
                    else if (body[i].operationtype == "修改")
                    {
                        //读取autoid
                        int icusautoid = Tardb.Queryable<SA_CusUPrice>().Where(x=>x.cCusCode== sA_CusUPrice.cCusCode && x.cInvCode == sA_CusUPrice.cInvCode && x.dStartDate == sA_CusUPrice.dStartDate && x.fminquantity == sA_CusUPrice.fminquantity)
                                .Select(x => x.AutoID).First();
                            sA_CusUPrice.AutoID = icusautoid;
                        cusPriceEntityModify.Add(sA_CusUPrice);

                    }
                }
             


                   

                        Tardb.BeginTran();
                        Tardb.Insertable(head).ExecuteCommand();
                        Tardb.Insertable(body).ExecuteCommand();
                        Tardb.Insertable(cusPriceEntity).ExecuteCommand();
                    Tardb.Updateable(cusPriceEntityModify).ExecuteCommand();
                    Tardb.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        Tardb.RollbackTran();
                        throw ex;
                    }
                    finally
                    {
                        // 显式关闭连接
                        Tardb.Ado.Connection?.Close();
                    }
              


                return DBService.ReOKObject("添加完成");
                //}
            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }



        }


        /// <summary>
        /// 检查单据号是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Exists(string ccode)
        {
           

            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
             bool ret =db.Queryable<SA_CusPriceJustMain>().Where(x => x.ccode == ccode).Select(x => x.id).Any();

             return ret;
            }

        }


        /// <summary>
        /// 检查价格表是否存在，根据客户，存货，启用日期和最小数量一起判断
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ExistsCusPrice(string ccuscode,string cinvcode,DateTime dbegindate,decimal fminqty)
        {
           
            
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
               bool bexists =  db.Queryable<SA_CusUPrice>().Where(x => x.cCusCode == ccuscode && x.cInvCode == cinvcode && x.dStartDate ==dbegindate && x.fminquantity ==fminqty).Any();
               return bexists;
    
            } 
        
        }

         
    }
}
